package com.app.actions.basesetting;

import com.app.SpringAnnotationDef;
import com.app.actions.BaseAction;
import com.app.model.Goods_Info;
import com.app.model.Goods_InfoToPackage;
import com.app.model.Goods_Package;
import com.app.model.Goods_Type;
import com.app.service.basesetting.GoodsService;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: cnprinces
 * Date: 13-1-25
 * Time: 下午2:21
 * To change this template use File | Settings | File Templates.
 */
@Scope(SpringAnnotationDef.SCOPE_PROTOTYPE)
@Component
@Namespace("/basesetting")
public class PackageAction extends BaseAction {
    private static final Logger LOGGER = Logger.getLogger(GoodsAction.class);

    @Resource(name = SpringAnnotationDef.GOODS_SERVICE)
    private GoodsService service;
    /**
     * 商品
     */
    private Goods_Info goodsInfo;

    /**
     * 套系名字
     */
    private Goods_Package goodsPackage;


    /**
     * 商品idlist
     */
    private String[] goodsList;

    public String[] getGoodsList() {
        return goodsList;
    }

    public void setGoodsList(String[] goodsList) {
        this.goodsList = goodsList;
    }

    public Goods_Info getGoodsInfo() {
        return goodsInfo;
    }

    public void setGoodsInfo(Goods_Info goodsInfo) {
        this.goodsInfo = goodsInfo;
    }

    public Goods_Package getGoodsPackage() {
        return goodsPackage;
    }

    public void setGoodsPackage(Goods_Package goodsPackage) {
        this.goodsPackage = goodsPackage;
    }

    @Action(value = "package", results = {@Result(name = "menuInfo", location = "/basesetting/goods/getGoodsPackagelist.jsp"),
            @Result(name = "addGoodsPackage", location = "/basesetting/goods/addGoodsPackage.jsp"),
            @Result(name = "selectGoods", location = "/basesetting/goods/selectGoods.jsp"),
            @Result(name = "goodsInfo", location = "/basesetting/goods/goodsInfo.jsp"),
            @Result(name = SUCCESS, location = "/basesetting/goods/goodsPackagelist.jsp")})
    public String getGoods() {

        List<Goods_Package> list = service.getDao().getGoodsPackageList(true);

        getRequest().setAttribute("list", list);

        return SUCCESS;
    }


    public String menuInfo() {
        if (goodsPackage != null) {
            goodsPackage = service.getDao().getGoodsPackageById(goodsPackage);
            //加载该套系下的所有的商品配置
            List<Goods_Info> goods_infos = service.loadGoodsInfoByPackageId(goodsPackage.getId());
            getRequest().setAttribute("goodsList",goods_infos);
        }
        return "menuInfo";
    }

    public String toAdd() {

        if (goodsPackage != null) {
            goodsPackage = service.getDao().getGoodsPackageById(goodsPackage);

        }

        return "addGoodsPackage";
    }


    public void addGoodsPackage() {
        if (goodsPackage != null) {
            goodsPackage.setInputId(getSessionLoginedUser().getId());
            goodsPackage.setInputName(getSessionLoginedUser().getEname());
            if (goodsPackage.getId() == null)
                goodsPackage.setCreateDate(new Date());
            service.getDao().saveObject(goodsPackage);
            JsonOut("添加成功", "goodsType");
        } else {
            JsonOut("添加失败", "goodsType");
        }
    }


    public void delGoodsPackage() {
        if (goodsPackage != null) {
            service.getDao().deleteGoodsPackage(goodsPackage);
            JsonOut("删除成功", "goodsType");
        } else {
            JsonOut("删除失败", "goodsType");
        }
    }


    /**
     * 选择商品
     *
     * @return
     */
    public String toSelectGoods() {
        //保存那个
        if (goodsPackage != null) {
            goodsPackage = service.getDao().getGoodsPackageById(goodsPackage);
        }

        //取得所有的商品
        List<Goods_Info> goods_infos = service.loadGoodsInfoList();
        getRequest().setAttribute("goodsList", goods_infos);
        //获得所有的资源菜单
        List<Goods_Type> list = service.getDao().listAll();

        //往request里放attribute
        getRequest().setAttribute("list", list);

        //取得父节点               addresources
        List<Goods_Type> parentRes = new ArrayList<Goods_Type>();
        for (Goods_Type res : list) {
            if (res.getParentId() == 0) {
                parentRes.add(res);
            }
        }
        //加载该套系下的所有的商品配置
        List<Goods_Info> goods_infosBypackage = service.loadGoodsInfoByPackageId(goodsPackage.getId());
        getRequest().setAttribute("goodsByPackage",goods_infosBypackage);

        getRequest().setAttribute("parentRes", parentRes);
        getRequest().setAttribute("goodsInfos", goods_infos);
        return "selectGoods";
    }


    /**
     * 保存关系
     */
    public void selectGoods() {

        if (goodsList != null && goodsPackage != null) {
            //先删除 todo:还没实现删除的方法
             service.getDao().deleteGoodsInfoToPackageByPackageId(goodsPackage.getId());
            //再保存
            for (String goodId : goodsList) {
                Goods_InfoToPackage infoToPackage = new Goods_InfoToPackage();
                infoToPackage.setGoodsPackageId(goodsPackage.getId());
                infoToPackage.setGoodsinfoId(Integer.parseInt(goodId));
                service.getDao().saveObject(infoToPackage);
            }
            JsonOut("添加成功", "goodsType");
        } else {
            JsonOut("删除失败", "goodsType");
        }
    }

    public String goodsInfo(){
        if(goodsInfo!=null){
            goodsInfo=service.getDao().getGoodsInfo(goodsInfo.getId());
        }
        return "goodsInfo";
    }
}
